package com.chinaums.itp.demo;

import com.chinaums.itp.client.ItpClient;
import com.chinaums.itp.client.ItpException;
import com.chinaums.itp.client.ItpMessage;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 发送报文
 *
 * Created on 2014-4-22.
 *
 * @author YaoFeng
 */
@Component
public class MessageSender {

	@Resource
	private ItpClient itpClient;

	@PostConstruct
	public void init() {
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				try {
					send_1101();
					send_2101();
					send_2201();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, 3000); // 启动3秒后自动发起测试交易
	}

	/** 查询代收交易结果。 */
	public void send_1101() throws ItpException, InterruptedException {
		final ItpMessage request = new ItpMessage();
		request.setTranId("DEMO_" + String.valueOf(System.currentTimeMillis()));
		request.setTranTime(new Date());
		request.setTranCode("1101");
		request.setBody("origSysTranId", "PRA000000536735");
		request.setBody("remark", "ITP客户端使用方法演示");

		System.out.printf("【同步请求】%n%s%n", MessageUtils.formatMessage(request));
		final ItpMessage response = itpClient.send(request);
		if (response != null) {
			System.out.printf("【同步应答】%n%s%n", MessageUtils.formatMessage(response));
		}
	}

	/** 发送单笔代收交易。 */
	public void send_2101() throws ItpException, InterruptedException {
		final ItpMessage request = new ItpMessage();
		request.setTranId("DEMO_" + String.valueOf(System.currentTimeMillis()));
		request.setTranTime(new Date());
		request.setTranCode("2101");
		request.setBody("currency", "RMB");
		request.setBody("sndAcctNo", "6222600790003902974");
		request.setBody("sndAcctName", "周宝治");
		request.setBody("sndBankCode", "03083930");
		request.setBody("sndBankName", "招商银行");
		request.setBody("tranAmt", "100");
		request.setBody("chkFlag", "0");
		request.setBody("busiType", "DEMO-TEST");
		request.setBody("merNo", "888350299999999");
		request.setBody("merName", "ITP客户端演示商户");
		request.setBody("tranChannel", "51"); //51代表交行渠道
		request.setBody("remark", "ITP客户端使用方法演示");

		System.out.printf("【同步请求】%n%s%n", MessageUtils.formatMessage(request));
		final ItpMessage response = itpClient.send(request);
		if (response != null) {
			System.out.printf("【同步应答】%n%s%n", MessageUtils.formatMessage(response));
		}
	}

	/** 发送单笔代付交易。 */
	public void send_2201() throws ItpException, InterruptedException {
		final ItpMessage request = new ItpMessage();
		request.setTranId("DEMO_" + String.valueOf(System.currentTimeMillis()));
		request.setTranTime(new Date());
		request.setTranCode("2201");
		request.setBody("currency", "RMB");
		request.setBody("rcvAcctNo", "1111222233334444");
		request.setBody("rcvAcctName", "张三");
		request.setBody("tranAmt", "100");
		request.setBody("chkFlag", "0");
		request.setBody("busiType", "DEMO-TEST");
		request.setBody("merNo", "888350299999999");
		request.setBody("merName", "ITP客户端演示商户");
		request.setBody("remark", "ITP客户端使用方法演示");

		System.out.printf("【同步请求】%n%s%n", MessageUtils.formatMessage(request));
		final ItpMessage response = itpClient.send(request);
		if (response != null) {
			System.out.printf("【同步应答】%n%s%n", MessageUtils.formatMessage(response));
		}
	}
}
